# macro for getting subdirectories in a directory
MACRO(GET_SUBDIR_LIST RESULT_LIST DIR)

	# get all items under given DIR
	FILE(GLOB ITEM_LIST RELATIVE ${DIR} ${DIR}/*)
	SET(FOLDER_LIST "")

	FOREACH(ITEM ${ITEM_LIST})
		
		# if directory add to folder list
		IF(IS_DIRECTORY ${DIR}/${ITEM})
			LIST(APPEND FOLDER_LIST ${ITEM})
		ENDIF()
	
	ENDFOREACH()
	
	# set folder list to result
	SET(${RESULT_LIST} ${FOLDER_LIST})

ENDMACRO()


# check for required cmake version
MACRO(CMAKE_VERSION_CHECK)

	CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)

ENDMACRO()

# Checks if the  SEARCH_VAR is in the SOURCE_LIST or not
# Also sets the CHECK_RESULT to true if given SOURCE_LIST is not defined.
# It is done since SOURCE_LIST is an optional parameter such as PLATFORMS, COMPILERS, etc.
MACRO(LIST_CHECK SOURCE_LIST SEARCH_VAR CHECK_RESULT)

	SET(${CHECK_RESULT} FALSE)
	
	# if list is defined
	IF (${SOURCE_LIST})
		
		# find G_TARGET_PLATFORM in given list
		LIST (FIND ${SOURCE_LIST} "${SEARCH_VAR}" TMP_CHECK)
		
		# if found, set result to true
		IF (TMP_CHECK GREATER -1)
			
			SET(${CHECK_RESULT} TRUE)
			
		ENDIF()
		
	ELSE()
	
		# if list is not defined then also set to true
		SET(${CHECK_RESULT} TRUE)
	
	ENDIF()
	
ENDMACRO(LIST_CHECK)


# Gives fatal error if ERROR_COND is false or not defined
MACRO(FILE_EXISTS FILE_NAME FOLDER_PATH)

	# find full path of FILE_NAME and store into ${FILE_NAME}_EXISTS
	FIND_FILE(${FILE_NAME}_EXISTS ${FILE_NAME} PATHS ${FOLDER_PATH})
	
	# check if ${FILE_NAME}_EXISTS is defined / file found
	IF (NOT ${FILE_NAME}_EXISTS)
	
		MESSAGE(FATAL_ERROR "${FILE_NAME} does not exist at ${FOLDER_PATH}.")
	
	ENDIF()
	
	# remove from cache
	UNSET(${FILE_NAME}_EXISTS CACHE)
	
ENDMACRO(FILE_EXISTS)


# CHECK_BUILD_PATH(<errorMessage>)
# Call this macro in your project if you want to enforce out-of-source builds.
# If an in-source build is detected, it will abort with the given error message.
# This macro works in any of the CMakeLists.txt of your project, but the recommended
# location to call this is close to the beginning of the top level CMakeLists.txt

# Copyright (c) 2006, Alexander Neundorf, <neundorf@kde.org>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.

MACRO(CHECK_BUILD_PATH)
    
	STRING(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" IN_SOURCE)
	GET_FILENAME_COMPONENT(PARENTDIR ${CMAKE_SOURCE_DIR} PATH)
	STRING(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${PARENTDIR}" IN_SOURCE_SUBDIR)
	
	IF(IN_SOURCE OR IN_SOURCE_SUBDIR)
		MESSAGE(FATAL_ERROR "Only out of source build is supported.")
	ENDIF()
	
ENDMACRO(CHECK_BUILD_PATH)


# The PARSE_ARGUMENTS macro will take the arguments of another macro and
# define several variables. The first argument to PARSE_ARGUMENTS is a
# prefix to put on all variables it creates. The second argument is a
# list of names, and the third argument is a list of options. Both of
# these lists should be quoted. The rest of PARSE_ARGUMENTS are
# arguments from another macro to be parsed.
#
#     PARSE_ARGUMENTS(prefix arg_names options arg1 arg2...)
#
# For each item in options, PARSE_ARGUMENTS will create a variable with
# that name, prefixed with prefix_. So, for example, if prefix is
# MY_MACRO and options is OPTION1;OPTION2, then PARSE_ARGUMENTS will
# create the variables MY_MACRO_OPTION1 and MY_MACRO_OPTION2. These
# variables will be set to true if the option exists in the command line
# or false otherwise.
#
# For each item in arg_names, PARSE_ARGUMENTS will create a variable
# with that name, prefixed with prefix_. Each variable will be filled
# with the arguments that occur after the given arg_name is encountered
# up to the next arg_name or the end of the arguments. All options are
# removed from these lists. PARSE_ARGUMENTS also creates a
# prefix_DEFAULT_ARGS variable containing the list of all arguments up
# to the first arg_name encountered.
	
#NOTE: USED LIST(FIND) instead of LIST_CONTAINS()

MACRO(PARSE_ARGUMENTS prefix arg_names option_names)
	
	SET(DEFAULT_ARGS)
	
	FOREACH(arg_name ${arg_names})
		SET(${prefix}_${arg_name})
	ENDFOREACH(arg_name)
	
	FOREACH(option ${option_names})
		SET(${prefix}_${option} FALSE)
	ENDFOREACH(option)

	SET(current_arg_name DEFAULT_ARGS)
	SET(current_arg_list)
	
	FOREACH(arg ${ARGN})
		SET (larg_names ${arg_names})
		LIST (FIND larg_names "${arg}" is_arg_name)
		
		IF (is_arg_name GREATER -1)
			SET(${prefix}_${current_arg_name} ${current_arg_list})
			SET(current_arg_name ${arg})
			SET(current_arg_list)
		ELSE (is_arg_name GREATER -1)
			SET (loption_names ${option_names})
			LIST (FIND loption_names "${arg}" is_option)
			
			IF (is_option GREATER -1)
				SET(${prefix}_${arg} TRUE)
			ELSE (is_option GREATER -1)
				SET(current_arg_list ${current_arg_list} ${arg})
			ENDIF (is_option GREATER -1)
		ENDIF (is_arg_name GREATER -1)
	ENDFOREACH(arg)
	
	SET(${prefix}_${current_arg_name} ${current_arg_list})
	
ENDMACRO(PARSE_ARGUMENTS)


# - Extract information from a subversion working copy
# The module defines the following variables:
#  Subversion_SVN_EXECUTABLE - path to svn command line client
#  Subversion_VERSION_SVN - version of svn command line client
#  Subversion_FOUND - true if the command line client was found
#  SUBVERSION_FOUND - same as Subversion_FOUND, set for compatiblity reasons
#
# The minimum required version of Subversion can be specified using the
# standard syntax, e.g. FIND_PACKAGE(Subversion 1.4)
#
# If the command line client executable is found two macros are defined:
#  Subversion_WC_INFO(<dir> <var-prefix>)
#  Subversion_WC_LOG(<dir> <var-prefix>)
# Subversion_WC_INFO extracts information of a subversion working copy at
# a given location. This macro defines the following variables:
#  <var-prefix>_WC_URL - url of the repository (at <dir>)
#  <var-prefix>_WC_ROOT - root url of the repository
#  <var-prefix>_WC_REVISION - current revision
#  <var-prefix>_WC_LAST_CHANGED_AUTHOR - author of last commit
#  <var-prefix>_WC_LAST_CHANGED_DATE - date of last commit
#  <var-prefix>_WC_LAST_CHANGED_REV - revision of last commit
#  <var-prefix>_WC_INFO - output of command `svn info <dir>'
# Subversion_WC_LOG retrieves the log message of the base revision of a
# subversion working copy at a given location. This macro defines the
# variable:
#  <var-prefix>_LAST_CHANGED_LOG - last log of base revision
# Example usage:
#  FIND_PACKAGE(Subversion)
#  IF(SUBVERSION_FOUND)
#    Subversion_WC_INFO(${PROJECT_SOURCE_DIR} Project)
#    MESSAGE("Current revision is ${Project_WC_REVISION}")
#    Subversion_WC_LOG(${PROJECT_SOURCE_DIR} Project)
#    MESSAGE("Last changed log is ${Project_LAST_CHANGED_LOG}")
#  ENDIF(SUBVERSION_FOUND)

#=============================================================================
# Copyright 2006-2009 Kitware, Inc.
# Copyright 2006 Tristan Carel
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
#  License text for the above reference.)

FIND_PROGRAM(Subversion_SVN_EXECUTABLE svn DOC "subversion command line client")
MARK_AS_ADVANCED(Subversion_SVN_EXECUTABLE)

IF(Subversion_SVN_EXECUTABLE)
	# the subversion commands should be executed with the C locale, otherwise
	# the message (which are parsed) may be translated, Alex
	SET(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
	SET(ENV{LC_ALL} C)

	EXECUTE_PROCESS(COMMAND ${Subversion_SVN_EXECUTABLE} --version 
		OUTPUT_VARIABLE Subversion_VERSION_SVN 
		OUTPUT_STRIP_TRAILING_WHITESPACE)

	# restore the previous LC_ALL
	SET(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})

	STRING(REGEX REPLACE "^(.*\n)?svn, version ([.0-9]+).*" 
		"\\2" Subversion_VERSION_SVN "${Subversion_VERSION_SVN}")

	MACRO(Subversion_WC_INFO dir prefix)
	
		# the subversion commands should be executed with the C locale, otherwise
		# the message (which are parsed) may be translated, Alex
		SET(_Subversion_SAVED_LC_ALL "$ENV{LC_ALL}")
		SET(ENV{LC_ALL} C)

		EXECUTE_PROCESS(COMMAND ${Subversion_SVN_EXECUTABLE} info ${dir}
			OUTPUT_VARIABLE ${prefix}_WC_INFO
			ERROR_VARIABLE Subversion_svn_info_error
			RESULT_VARIABLE Subversion_svn_info_result
			OUTPUT_STRIP_TRAILING_WHITESPACE)

		IF(NOT ${Subversion_svn_info_result} EQUAL 0)
			MESSAGE(WARNING "Command \"${Subversion_SVN_EXECUTABLE} info ${dir}\" failed with output:\n${Subversion_svn_info_error}")
			SET(${prefix}_WC_RESULT FALSE)
			SET(${prefix}_WC_URL "")
			SET(${prefix}_WC_ROOT "")
			SET(${prefix}_WC_REVISION "0")
			SET(${prefix}_WC_LAST_CHANGED_AUTHOR "")
			SET(${prefix}_WC_LAST_CHANGED_REV "0")
			SET(${prefix}_WC_LAST_CHANGED_DATE "")
		ELSE(NOT ${Subversion_svn_info_result} EQUAL 0)
			SET(${prefix}_WC_RESULT TRUE)
			STRING(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
			"\\2" ${prefix}_WC_URL "${${prefix}_WC_INFO}")
			STRING(REGEX REPLACE "^(.*\n)?Repository Root: ([^\n]+).*"
			"\\2" ${prefix}_WC_ROOT "${${prefix}_WC_INFO}")
			STRING(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
			"\\2" ${prefix}_WC_REVISION "${${prefix}_WC_INFO}")
			STRING(REGEX REPLACE "^(.*\n)?Last Changed Author: ([^\n]+).*"
			"\\2" ${prefix}_WC_LAST_CHANGED_AUTHOR "${${prefix}_WC_INFO}")
			STRING(REGEX REPLACE "^(.*\n)?Last Changed Rev: ([^\n]+).*"
			"\\2" ${prefix}_WC_LAST_CHANGED_REV "${${prefix}_WC_INFO}")
			STRING(REGEX REPLACE "^(.*\n)?Last Changed Date: ([^\n]+).*"
			"\\2" ${prefix}_WC_LAST_CHANGED_DATE "${${prefix}_WC_INFO}")

		ENDIF(NOT ${Subversion_svn_info_result} EQUAL 0)

		# restore the previous LC_ALL
		SET(ENV{LC_ALL} ${_Subversion_SAVED_LC_ALL})

	ENDMACRO(Subversion_WC_INFO)

	MACRO(Subversion_WC_LOG dir prefix)
	
		# This macro can block if the certificate is not signed:
		# svn ask you to accept the certificate and wait for your answer
		# This macro requires a svn server network access (Internet most of the time)
		# and can also be slow since it access the svn server
		EXECUTE_PROCESS(COMMAND
			${Subversion_SVN_EXECUTABLE} --non-interactive log -r BASE ${dir}
			OUTPUT_VARIABLE ${prefix}_LAST_CHANGED_LOG
			ERROR_VARIABLE Subversion_svn_log_error
			RESULT_VARIABLE Subversion_svn_log_result
			OUTPUT_STRIP_TRAILING_WHITESPACE)

		IF(NOT ${Subversion_svn_log_result} EQUAL 0)
			MESSAGE(WARNING "Command \"${Subversion_SVN_EXECUTABLE} log -r BASE ${dir}\" failed with output:\n${Subversion_svn_log_error}")
		ENDIF(NOT ${Subversion_svn_log_result} EQUAL 0)
		
	ENDMACRO(Subversion_WC_LOG)

ELSE()
	
	MACRO(Subversion_WC_INFO dir prefix)
		SET(${prefix}_WC_RESULT FALSE)
    	SET(${prefix}_WC_URL "")
    	SET(${prefix}_WC_ROOT "")
    	SET(${prefix}_WC_REVISION "0")
    	SET(${prefix}_WC_LAST_CHANGED_AUTHOR "")
    	SET(${prefix}_WC_LAST_CHANGED_REV "0")
    	SET(${prefix}_WC_LAST_CHANGED_DATE "")
	ENDMACRO()
	
 	MACRO(Subversion_WC_LOG dir prefix)
	ENDMACRO()
	
ENDIF(Subversion_SVN_EXECUTABLE)

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Subversion REQUIRED_VARS Subversion_SVN_EXECUTABLE
                                             VERSION_VAR Subversion_VERSION_SVN )

# for compatibility
SET(Subversion_FOUND ${SUBVERSION_FOUND})
SET(Subversion_SVN_FOUND ${SUBVERSION_FOUND})